module RailsNavigation
  module Matcher
    def self.check_controller(controller_name, str)
      result = Array(str.match(/^([a-z_\-]+)/))[1]
      result ? (result == controller_name) : true
    end

    def self.check_action(action_name, str)
      result = Array(str.match(/#([a-z_\-]+)/))[1]
      result ? (result == action_name) : true
    end

    def self.check_parameters(params, str)
      result = Array(str.match(/\?([!a-z0-9&_\-=]+)/))[1]
      if result
        key, value = result.split('=')
        result.include?('=') ? value == params[key.to_sym] : result == params[:id]
      else
        true
      end
    end

    def self.match_single?(controller_name, action_name, params, str)
      check_controller(controller_name, str) && check_action(action_name, str) && check_parameters(params, str)
    end

    def self.match_multiple_with_or?(controller_name, action_name, params, arr)
      [*arr].any? { |str| match_single?(controller_name, action_name, params, str) }
    end
  end
end
